package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.demo.entity.Person;
import com.example.demo.entity.Sysuser;
import com.example.demo.service.ISysuserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private ISysuserService iSysuserService;

    @RequestMapping("/getjson")
    @ResponseBody
    public String getUser(){
        //EntityWrapper条件构造器
        EntityWrapper con=new EntityWrapper<Sysuser>();
        con.eq("id",1);

        //查询一条数据
        Sysuser oneData=iSysuserService.selectOne(con);

        //String jsonStr= oneData.toString();
        //System.out.println(jsonStr);

        String RE=JSON.toJSONString(oneData);
        String jsonStr=oneData.toString();
        System.out.println(jsonStr);

        return RE;
    }

    private List<Person> listofPersons=new ArrayList<Person>();

    @RequestMapping("/fastjsontostr")
    @ResponseBody
    public JSONArray jsonben(){
        listofPersons.add(new Person(12,"john doe",new Date()));
        listofPersons.add(new Person(15,"janette doe",new Date()));
        String result=JSON.toJSONString(listofPersons);

        //json字符转成java对象
        Person personOne=new Person(10,"张三",new Date());

        //对象转json字符串
        String personOneStr=JSON.toJSONString(personOne);

        //json字符串转对象
        Person newPerson=JSON.parseObject(personOneStr,Person.class);

        //获取对象值
        String fullname=newPerson.getFullName();

        JSONArray array_data=new JSONArray();
        array_data.add(newPerson);

        return array_data;
    }
}
